using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Objects;
using FAQComponents.API;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for FAQAdmin.
	/// </summary>
	public partial class FAQAdmin : BidirectionalSortableDataGridPage
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				BindData();
				lnkCreateFAQ.Visible = FAQContext.GetContext().LoggedOnUser.Permissions.Count > 0
					|| FAQContext.GetContext().LoggedOnUser.IsAdmin;
			}
		}

		CategoryCollection allCategories = null;
		private void BindData()
		{
			allCategories = CategoryAPI.GetAllCategories();

			User u = FAQContext.GetContext().LoggedOnUser;
			FAQCollection myFAQs = FAQAPI.GetFAQsByUser(u.UserID);

			base.BindData(dgOwnFAQs, myFAQs);

            dgOwnFAQs.Visible = myFAQs.Count > 0;
			noOwnFAQsMessage.Visible = myFAQs.Count == 0;


			FAQPartCollection contributedFAQs = FAQAPI.GetFAQContributions(u.UserID);
			base.BindData(dgContributedFAQs, contributedFAQs);

			dgContributedFAQs.Visible = contributedFAQs.Count > 0;
			noContributionsMessage.Visible = contributedFAQs.Count == 0;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgOwnFAQs.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgOwnFAQs_PageIndexChanged);
			this.dgOwnFAQs.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgOwnFAQs_SortCommand);
			this.dgOwnFAQs.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgOwnFAQs_DeleteCommand);
			this.dgOwnFAQs.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgOwnFAQs_ItemDataBound);
			this.dgContributedFAQs.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgContributedFAQs_PageIndexChanged);
			this.dgContributedFAQs.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgContributedFAQs_SortCommand);
			this.dgContributedFAQs.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgContributedFAQs_DeleteCommand);
			this.dgContributedFAQs.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgContributedFAQs_ItemDataBound);

		}
		#endregion

		#region dgOwnFAQs DataGrid Event Handlers
		private void dgOwnFAQs_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				DropDownList categories = (DropDownList) e.Item.FindControl("categories");
				categories.DataSource = allCategories;
				categories.DataBind();

				string faqCategoryID = DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString();
				ListItem li = categories.Items.FindByValue(faqCategoryID);
				if (li != null) li.Selected = true;

				// add a confirm dialog to the delete button
				LinkButton db = (LinkButton) e.Item.Cells[2].Controls[0];
				db.Attributes["onclick"] = "javascript:if (!confirm('This will permanently delete this FAQ and all FAQ parts within.  Are you sre you want to do this?')) return false; else return confirm('Are you really sure?  I mean, 100%, absolutely, positively sure?');";
			}
		}

		private void dgOwnFAQs_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			base.HandlePaging(dgOwnFAQs, e.NewPageIndex);
			BindData();
		}

		private void dgOwnFAQs_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			base.HandleSorting(dgOwnFAQs, e.SortExpression);
			BindData();
		}

		private void dgOwnFAQs_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int FAQID = (int) dgOwnFAQs.DataKeys[e.Item.ItemIndex];
			FAQAPI.DeleteFAQ(FAQID);

			// move back a page, if needed
			if (dgOwnFAQs.Items.Count == 1)
				dgOwnFAQs.CurrentPageIndex = Math.Max(0, dgOwnFAQs.CurrentPageIndex - 1);

			BindData();
		}
		#endregion

		#region dgContributedFAQs DataGrid Event Handlers
		private void dgContributedFAQs_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int FAQPartID = (int) dgContributedFAQs.DataKeys[e.Item.ItemIndex];
			FAQAPI.DeleteFAQPart(FAQPartID);

			// move back a page, if needed
			if (dgContributedFAQs.Items.Count == 1)
				dgContributedFAQs.CurrentPageIndex = Math.Max(0, dgContributedFAQs.CurrentPageIndex - 1);

			BindData();	
		}

		private void dgContributedFAQs_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				DropDownList categories = (DropDownList) e.Item.FindControl("contributionCategories");
				categories.DataSource = allCategories;
				categories.DataBind();

				string faqCategoryID = DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString();
				ListItem li = categories.Items.FindByValue(faqCategoryID);
				if (li != null) li.Selected = true;

				// add a confirm dialog to the delete button
				LinkButton db = (LinkButton) e.Item.Cells[1].Controls[0];
				db.Attributes["onclick"] = "javascript:if (!confirm('This will permanently delete this FAQ part.  Are you sre you want to do this?')) return false; else return confirm('Are you really sure?  I mean, 100%, absolutely, positively sure?');";
			}
		}

		private void dgContributedFAQs_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			base.HandlePaging(dgOwnFAQs, e.NewPageIndex);
			BindData();
		}

		private void dgContributedFAQs_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			base.HandleSorting(dgOwnFAQs, e.SortExpression);
			BindData();
		}
		#endregion
	}
}
